home *** CD-ROM | disk | FTP | other *** search
- Path: apccorp.apcc.com!root
- From: nfegan@apcc.com (Noel Fegan)
- Newsgroups: comp.lang.c++
- Subject: Re: Help with bizzare bug (Long)
- Date: Wed, 28 Feb 1996 10:04:00 GMT
- Organization: American Power Conversion
- Message-ID: <4h19bq$m3m@apccorp.apcc.com>
- References: <tday-2702962256130001@tday.slip.netcom.com>
- NNTP-Posting-Host: hewie.galway.apcc.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- tday@netcom.com (Tony Day) wrote:
-
- >>NameAr & NameAr::operator=(const NameAr & a)
- >>{
- >> if (this == &a) // if object assigned to self,
- >> return *this; // don't change anything
- >> ArrayDb::operator=(a);
- >> delete [] name;
- >> name = new char[strlen(a.name)+1];
- >> strcpy(name, a.name);
- >> return *this;
- >>}
-
- >and the offending statement is "delete [] name" (if it is removed the
- >program runs as wriiten)
-
- >
- >NameAr::NameAr()
- >{
- > name = new char[strlen("Smiley")+1];
- > name = "Smiley";
- >}
-
- >
- >NameAr::NameAr(const ArrayDb & a): ArrayDb(a)
- >{
- > name = new char[strlen("Smiley")+1];
- > name = "Smiley";
-
- >}
-
- The two constructors above set "name" to point to the character string "Smiley"
- in both cases. However, this did no involve newing memory and copying the string
- into the new'ed block. So when the NameAr::operator= function is called it tries
- to delete the character array "Smiley" which it has no business doing because
- the block was not newed. The destructor of NameAr would also have a problem.
- --
- Noel Fegan
- American Power Conversion
-
-